package com.wangtao.firstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.AllBatheAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.custom.ScRecyclerView;
import com.wangtao.firstapp.modles.AllBatheData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 全部晒单页面
 * Created by lanouhn on 16/9/11.
 */
public class AllBatheActivity extends Activity {

    private AllBatheAdapter allBatheAdapter;
    private ScRecyclerView allBathe_ScRecyclerView;
    private AllBatheData allBatheData;

    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allbathe);
        initView();
        getDataFromWeb();
        initEvent();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.back_act_allbathe);//左上角的返回
        allBathe_ScRecyclerView = (ScRecyclerView) findViewById(R.id.allbathe_ScRecyclerView_act_allbathe);
    }

    private void getDataFromWeb() {
        Intent intent = getIntent();
        String details_ID = intent.getStringExtra("details_iD");

        String urlAllBathe = "http://open4.bantangapp.com/products/brandProduct/relationPostList";
        String paramsAllBathe = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=10&id=" + details_ID;

        try {
            HttpUtils.doPostAsyn(urlAllBathe, paramsAllBathe, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_AllBathe = new Gson();
                    allBatheData = gson_AllBathe.fromJson(result, AllBatheData.class);
                    if (allBatheData.getData() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    allBatheAdapter = new AllBatheAdapter(AllBatheActivity.this, allBatheData);
                    allBathe_ScRecyclerView.setAdapter(allBatheAdapter);
                    allBathe_ScRecyclerView.setLayoutManager(new FullyLinearLayoutManager(AllBatheActivity.this));
                    break;
            }
        }
    };

    private void initEvent() {
        //返回上一个页面
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
